package wxpay.app.util;



import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;



import util.MD5Util;
import wxpay.web.services.ConfigUtil;




public class Sign {

	// 生成签名
	@SuppressWarnings("rawtypes")
	public static String createSign(String characterEncoding,
			SortedMap<Object, Object> parameters) {
		StringBuffer sb = new StringBuffer();
		Set es = parameters.entrySet();
		Iterator it = es.iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			String k = (String) entry.getKey();
			Object v = entry.getValue();
			if ((v == null) || ("".equals(v)) || ("sign".equals(k))
					|| ("key".equals(k)))
				continue;
			sb.append(k + "=" + v + "&");
		}
		sb.append("key=" + ConstantUtil.PARTNER_KEY);
	//	System.out.println("sb:"+sb.toString());
		String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding)
				.toUpperCase();
		return sign;
	}
	
	
	@SuppressWarnings("rawtypes")
	public static String addSign(String characterEncoding,
			SortedMap<Object, Object> parameters) {
		StringBuffer sb = new StringBuffer();
		Set es = parameters.entrySet();
		Iterator it = es.iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			String k = (String) entry.getKey();
			Object v = entry.getValue();
			if (null != v && !"".equals(v) && !"sign".equals(k)
					&& !"key".equals(k)) {
				sb.append(k + "=" + v + "&");
			}
		}
		// System.out.println("sb:"+sb.toString());
		sb.append("key=" + ConfigUtil.API_KEY);
		String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding)
				.toUpperCase();
		return sign;

		}
}